import type { RouteMetaType, RouteConfigType } from './types/index.type';
const nzhcn = require('nzh/cn'); //直接使用简体中文

// 数字转中文
export const dealBigMoney = (num: number, prefix = '人民币：') => {
  return nzhcn.toMoney(num).replace('人民币', prefix); // 默认人民币xxx
};

// 查找路由信息meta数据函数
export const findMeta = (routes: RouteConfigType[], pathname: string): RouteMetaType[] => {
  const pathnames = pathname.split('/');
  const metas: RouteMetaType[] = [];
  const searchRoutes = (currentRoutes: RouteConfigType[], currentPath: string): void => {
    for (const route of currentRoutes) {
      const fullPath = route.path || '';
      if ((fullPath === currentPath || fullPath === `/${currentPath}`) && route.meta) {
        metas.push(route.meta);
      }
      // 检查子路由
      if (route.children && Array.isArray(route.children)) {
        searchRoutes(route.children, currentPath);
      }
    }
  };
  for (const path of pathnames) {
    path && searchRoutes(routes, path);
  }
  // 返回找到的所有元数据，如果没有匹配项则返回空数组
  return metas;
};
